package com.yzf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.hzl.Constants;
import com.hzl.ContactsActivity;
import com.hzl.UserInfoBean;
import com.yao.component.circle.R;
import com.yzf.bean.GroupBean;
import java.util.ArrayList;

public class LabelMemberActivity extends Activity {
    private GroupBean groupBean = new GroupBean();
    private boolean isShowRed;
    private Button deleteBtn;
    //判断是添加还是新建
    private String type;
    private TextView titleTv;
    private ArrayList<UserInfoBean> list = new ArrayList<UserInfoBean>();
    private MyGridAdapter myGridAdapter;
    private boolean needjiajianImg;//是否要添加加减的图标
    private EditText labelEt;
    private int selectPosition;//从谁可以看的页面跳转过来，返回值的时候显示对应的item，1：显示部分可见，2：显示不给谁看
    private EditText mEditText;
    private ImageView deleteImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_label_member);
        type = getIntent().getStringExtra("type");
        groupBean = (GroupBean)getIntent().getSerializableExtra("groupBean");
        selectPosition = getIntent().getIntExtra("selectPosition", 0);
        if ("addLabel".equals(type)) {
            list = groupBean.getPersonList();
            if (list != null) {
                groupBean.setPersonList(list);
                ArrayList<UserInfoBean> personList = groupBean.getPersonList();
                personList.add(new UserInfoBean(0, R.drawable.add_little,"","",0,0));
                personList.add(new UserInfoBean(0, R.drawable.less_little,"","",0,0));
                groupBean.setPersonList(personList);
            }
        } else if("saveLabel".equals(type)){//发布界面跳转过来
            list = groupBean.getPersonList();
            if (list != null) {
                groupBean.setPersonList(list);
                ArrayList<UserInfoBean> personList = groupBean.getPersonList();
                personList.add(new UserInfoBean(0, R.drawable.add_little,"","",0,0));
                personList.add(new UserInfoBean(0, R.drawable.less_little,"","",0,0));
                groupBean.setPersonList(personList);
            }
        }else if ("showLabelMember".equals(type)) {//
            groupBean = (GroupBean) getIntent().getSerializableExtra("groupBean");
            ArrayList<UserInfoBean> personList = groupBean.getPersonList();
            personList.add(new UserInfoBean(0, R.drawable.add_little,"","",0,0));
            personList.add(new UserInfoBean(0, R.drawable.less_little,"","",0,0));
            groupBean.setPersonList(personList);
        }

        findViewById(R.id.label).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LabelMemberActivity.this.finish();
            }
        });
        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(labelEt.getText().toString())){
                    Toast.makeText(LabelMemberActivity.this, "请输入标签名字", Toast.LENGTH_SHORT).show();
                }else{
                    if("saveLabel".equals(type)){
                        Intent intent  = new Intent();
                        groupBean.setId(Data.id);
                        Data.id++;
                        groupBean.setName(labelEt.getText().toString());
                        intent.putExtra("groupBean",groupBean);
                        intent.putExtra("selectPosition",selectPosition);
                        setResult(Constants.NEW_LABEL_CODE,intent);
                        LabelMemberActivity.this.finish();
                    }else if("addLabel".equals(type)){
                        Intent intent  = new Intent();
                        groupBean.setId(Data.id);
                        Data.id++;
                        groupBean.setName(labelEt.getText().toString());
                        intent.putExtra("groupBean",groupBean);
                        setResult(Constants.ADD_LABEL_CODE,intent);
                        LabelMemberActivity.this.finish();
                    }else if("showLabelMember".equals(type)){
                        Intent intent  = new Intent();
                        groupBean.setName(labelEt.getText().toString());
                        intent.putExtra("groupBean",groupBean);
                        setResult(Constants.ADD_MEMBER_CODE,intent);
                        LabelMemberActivity.this.finish();
                    }
                }

            }
        });
        titleTv = (TextView) findViewById(R.id.label_title_tv);
        deleteBtn = (Button) findViewById(R.id.delete_btn);
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent  = new Intent();
                intent.putExtra("groupId",groupBean.getId()+"");
                setResult(Constants.DEL_MEMBER_CODE,intent);
                LabelMemberActivity.this.finish();
            }
        });
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(needjiajianImg){
            ArrayList<UserInfoBean> personList = groupBean.getPersonList();
            personList.add(new UserInfoBean(0, R.drawable.add_little,"","",0,0));
            personList.add(new UserInfoBean(0, R.drawable.less_little,"","",0,0));
            groupBean.setPersonList(personList);
            myGridAdapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        deleteImg = (ImageView)findViewById(R.id.memeberlabel_delete_img);
        deleteImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                labelEt.setText("");
            }
        });
        labelEt = (EditText) findViewById(R.id.label_et_name);
        labelEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String sss = labelEt.getText().toString();
                if("".equals(sss)){
                    deleteImg.setVisibility(View.INVISIBLE);
                }else{
                    deleteImg.setVisibility(View.VISIBLE);
                }
            }
        });
        if ("addLabel".equals(type)||"saveLabel".equals(type)) {//新建不显示删除按钮，编辑框提示：例如家人、朋友
            deleteBtn.setVisibility(View.INVISIBLE);
            titleTv.setText("新建标签");
        } else if ("showLabelMember".equals(type)) {
            deleteBtn.setVisibility(View.VISIBLE);
            labelEt.setText(groupBean.getName());
            titleTv.setText("标签成员");
        }
        MyGridView gridView = (MyGridView) findViewById(R.id.labelmemeber_gridview);
        myGridAdapter = new MyGridAdapter();
        gridView.setAdapter(myGridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击删除好友
                if (groupBean.getPersonList().size() > 2) {//删除好友
                    if (position == groupBean.getPersonList().size() - 1) {
                        isShowRed = !isShowRed;
                    }
                    if(position == groupBean.getPersonList().size()-2){//从同学录加好友
                        Intent intent = new Intent(LabelMemberActivity.this, ContactsActivity.class);
                        if(groupBean.getPersonList().size()>2){
                            ArrayList<UserInfoBean> personList = groupBean.getPersonList();
                            personList.remove(personList.size()-2);
                            personList.remove(personList.size()-1);
                            needjiajianImg = true;
                            groupBean.setPersonList(personList);
                            for(int i = 0;i<personList.size();i++){
                                personList.get(i).setStatus(3);
                            }
                            intent.putExtra("personList", groupBean.getPersonList());
                            intent.putExtra("type", Constants.ADD_LABEL);
                            startActivityForResult(intent,1);
                        }
                    }
                }
                myGridAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            if(requestCode == 1){
                needjiajianImg = false;
                ArrayList<UserInfoBean> userInfoBean = (ArrayList<UserInfoBean>) data.getSerializableExtra("UserInfoBean");
                list.clear();
                list.addAll(userInfoBean);
                if (list != null) {
                    groupBean.setPersonList(list);
                    ArrayList<UserInfoBean> personList = groupBean.getPersonList();
                    personList.add(new UserInfoBean(0, R.drawable.add_little,"","",0,0));
                    personList.add(new UserInfoBean(0, R.drawable.less_little,"","",0,0));
                    groupBean.setPersonList(personList);
                    myGridAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    class MyGridAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if(groupBean.getPersonList()==null){
                return 0;
            }else{
                return groupBean.getPersonList().size();
            }
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View inflate = getLayoutInflater().inflate(R.layout.gridview_item, null);
            final ArrayList<UserInfoBean> personList = groupBean.getPersonList();
            final UserInfoBean UserInfoBean = personList.get(position);
            ImageView img = (ImageView) inflate.findViewById(R.id.gd_img);
            img.setImageResource(UserInfoBean.getHeadImg());
            TextView tv = (TextView) inflate.findViewById(R.id.gd_tv);
            tv.setText(UserInfoBean.getUserName());
            Button red = (Button) inflate.findViewById(R.id.red);
            if (isShowRed) {
                if (position < groupBean.getPersonList().size() - 2) {
                    red.setVisibility(View.VISIBLE);
                }
            }
            red.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    personList.remove(UserInfoBean);
                    notifyDataSetChanged();
                }
            });
            return inflate;
        }
    }
}
